package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.PlatformTimeliness;
import com.qianying.service.PlatformTimelinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 平台时效表控制器（管理员接口）
 * 提供平台时效相关的CRUD接口
 *
 * @author check
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/admin/platformTimeliness")
@Tag(
        name = "平台时效管理（管理员）",
        description = "提供平台时效配置的查询、新增、更新和删除功能"
)
public class PlatformTimelinessController {

    @Autowired
    private PlatformTimelinessService platformTimelinessService;

    /**
     * 分页查询平台时效列表
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询时效列表",
            description = "支持按时效类型模糊查询，返回分页数据（含id、时效类型、值、描述等字段）",
            parameters = {
                    @Parameter(
                            name = "page",
                            description = "页码（≥1）",
                            required = true,
                            example = "1",
                            schema = @Schema(type = "integer", minimum = "1")
                    ),
                    @Parameter(
                            name = "size",
                            description = "每页条数（1-100）",
                            required = true,
                            example = "10",
                            schema = @Schema(type = "integer", minLength = 1, maxLength = 100)
                    ),
                    @Parameter(
                            name = "timelinessType",
                            description = "时效类型（模糊匹配）",
                            required = false,
                            example = "接单时效"
                    )
            }
    )
    public CommonResult<IPage<PlatformTimeliness>> getPage(
            @RequestParam Integer page,
            @RequestParam Integer size,
            @RequestParam(required = false) String timelinessType) {
        Page<PlatformTimeliness> pageParam = new Page<>(page, size);
        return platformTimelinessService.getPage(pageParam, timelinessType);
    }

    /**
     * 根据ID查询平台时效详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询时效详情",
            description = "通过ID获取单条时效配置的完整信息（含创建/更新时间）",
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "时效记录ID",
                            required = true,
                            example = "1",
                            schema = @Schema(type = "integer", format = "int64")
                    )
            }
    )
    public CommonResult<PlatformTimeliness> getById(@PathVariable Long id) {
        return platformTimelinessService.getById(id);
    }

    /**
     * 新增平台时效信息
     */
    @PostMapping("/saveTimeliness")
    @Operation(
            summary = "新增时效配置",
            description = "创建新时效配置，需提供类型和值（分钟），ID自动生成",
            parameters = {
                    @Parameter(
                            name = "timelinessType",
                            description = "时效类型（如：接单时效、设计时效等）",
                            required = true
                    ),
                    @Parameter(
                            name = "startNode",
                            description = "开始节点（时效起始阶段）",
                            required = true
                    ),
                    @Parameter(
                            name = "endNode",
                            description = "结束节点（时效完成阶段）",
                            required = true
                    ),
                    @Parameter(
                            name = "urgentTime",
                            description = "紧急单处理时间（小时）",
                            required = true
                    ),
                    @Parameter(
                            name = "normalTime",
                            description = "普通单处理时间（小时）",
                            required = true
                    )
            },
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "时效配置信息（不含ID）",
                    required = true,
                    content = @Content(schema = @Schema(implementation = PlatformTimeliness.class))
            )
    )
    public CommonResult<?> saveTimeliness(@RequestBody PlatformTimeliness timeliness) {
        return platformTimelinessService.saveTimeliness(timeliness);
    }

    /**
     * 更新平台时效信息
     */
    @PostMapping("/updateTimeliness")
    @Operation(
            summary = "更新时效配置",
            description = "修改已有配置，需包含ID，创建时间不可修改",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "时效配置信息（含ID）",
                    required = true,
                    content = @Content(schema = @Schema(implementation = PlatformTimeliness.class))
            ),
            responses = {
                    @ApiResponse(responseCode = "200", description = "更新成功"),
                    @ApiResponse(responseCode = "400", description = "参数错误"),
                    @ApiResponse(responseCode = "404", description = "记录不存在"),
                    @ApiResponse(responseCode = "500", description = "更新失败")
            }
    )
    public CommonResult<?> updateTimeliness(@RequestBody PlatformTimeliness timeliness) {
        return platformTimelinessService.updateTimeliness(timeliness);
    }

    /**
     * 根据ID删除平台时效
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除时效配置",
            description = "通过ID删除时效配置，操作不可恢复",
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "时效记录ID",
                            required = true,
                            example = "1",
                            schema = @Schema(type = "integer", format = "int64")
                    )
            }
    )
    public CommonResult<?> removeById(@PathVariable Long id) {
        return platformTimelinessService.removeById(id);
    }
}
